package com.atguigu.juc.threadLocal;

import java.util.Random;
import java.util.concurrent.TimeUnit;

/**
 * 需求一：5个销售卖房子，集团高层只关心销售总量的准确统计数。
 *
 * @author yfstart
 * @create 2022-07-23 16:58
 */
public class ThreadLocalDemo {
    public static void main(String[] args) {
        House house = new House();
        for (int i = 1; i <= 5; i++) {
            new Thread(() -> {
                int size = new Random().nextInt(5) + 1;
                for (int j = 0; j < size; j++) {
                    house.saleHouse();
                }
                System.out.println(Thread.currentThread().getName() + "\t 卖出 " + size + "套");
            }, String.valueOf(i)).start();
        }

        try { TimeUnit.MILLISECONDS.sleep(300); } catch (InterruptedException e) { e.printStackTrace();}

        System.out.println(Thread.currentThread().getName() + "\t 共计卖出 " + house.saleCount + " 套");
    }
}

/**
 * 资源类
 */
class House {
    int saleCount = 0;

    public synchronized void saleHouse() {
        ++saleCount;
    }
}